import { useRequest } from 'umi';
import { AssetListManageTab } from '../mconfig/ManageTabSwitch';
import { getBaseUrl } from '@/services/urlconst';
import { requestFunComponFiex } from '@/utils';
let baseUrl = getBaseUrl();
/** 获取折旧记录数据 */
const getGatherListSev = (cb, curRes) => {
  /**获取权限参数 */
  const queryTypeObj: Record<string, any> = AssetListManageTab(curRes);
  /**请求数据*/
  const { tableProps, data, run, loading, params, refresh }: Record<string, any> = useRequest(
    (opt: Record<string, any>) => {
      let reqUrl = `${baseUrl}/assetDepreciationCalculation/list`;
      let reqBody = {
        page: 1,
        rows: 10,
        ...opt,
        ...queryTypeObj,
        sort: 'deprecitionTime',
        order: 'desc',
      };
      return requestFunComponFiex('POST', reqUrl, reqBody);
    },
    {
      paginated: true,
      defaultPageSize: 10,
      onSuccess: cb,
    },
  );
  /**封装请求方法*/
  const getGatherListFun = (param) => {
    const reqBody = {
      ...param,
    };
    run({ ...reqBody });
  };

  return {
    getGatherListFun,
    reloadRequest: refresh,
    getGatherListLoading: loading,
    otherParams: params?.[0],
    gatherList: data?.rows || [],
    pagination: tableProps?.pagination,
  };
};

/** 删除折旧记录数据 */
const deletGatherListSev = (cb) => {
  const request = useRequest(
    (opt) => {
      if (!opt?.depreciationDate) return;
      let reqUrl = `${baseUrl}/assetDepreciationCalculation/deleteErrorDepreciation/${opt?.depreciationDate}`; //depreciationDate
      return requestFunComponFiex('DELETE', reqUrl);
    },
    {
      manual: true,
      onSuccess: cb,
    },
  );

  return {
    deletGatherListRun: request?.run,
    deletGatherListLoading: request?.loading,
  };
};

export { getGatherListSev, deletGatherListSev };
